$(function(){

	var timeLeft; //剩余时间
	var timeLeft1;

	var greenTime//绿灯时间
	var greenTime1
	var sign;//红黄绿灯标志
	var sign1;
	
	var direction;
	var direction1;
 	
 	var dataID;
 	var dataID1;

 	var positionX;
 	var positionY;

 	var lastX;//上一次的X与Y
 	var lastY;

 	var speed;//记录速度


	

 	window.setInterval(function(){
	 	receiveData();
	 	
	 },250)
 	window.setInterval(function(){
 		receiveLocation();
 	},1000);

	 //绘制画布
	 var context = $('#canvas')[0].getContext("2d");     
	 var myimg = new Image();
	 myimg.src="img/test.png";

	 myimg.onload=function(){
	  	context.drawImage(myimg,0,0);
	 }
	 
	   
	


	
	 //接收数据
	 function receiveData(){
	  	 $.ajax({
		      type: "get",
		      url: "http://182.92.6.59:8099/Bus/getData", 
		      dataType: "text",
		      contentType: "application/json",
		      success: function (data) {
		      		if(data=="无数据"){
		        		var lastData=sessionStorage.getItem('lastData');
		        		//$('#receive').text(lastData);
		        		direction1=sessionStorage.getItem('direction');
		        		dataID1=sessionStorage.getItem('dataID');
		        		sign1=sessionStorage.getItem('sign');
		        		greenTime1=sessionStorage.getItem('greenTime');
		  
		        	}else{
		        		//$('#receive').text(data);
		        		
		        		sessionStorage.setItem('lastData',data);
		        		
		        		//相阶ID
		        		direction=data.substring(48,50);
		        		//数据ID
		        		dataID=data.substring(32,36);
		        		//红绿灯标志
		        		sign=data.slice(-10,-8);
		        		sessionStorage.setItem("dataID",dataID);
		        		greenTime=data.slice(-8,-6);
		        		// console.log(direction);
		        		sessionStorage.setItem("direction",direction);
		        		
		        		sessionStorage.setItem("sign",sign);
		        		sessionStorage.setItem("greenTime",greenTime);
		        	}	
		        	if(dataID==8201){

		        	 	if(direction=='01'){//01竖直方向
		        			//变色
			        		if(sign=='01'||sign1=='01'){
			        			$('.leftLight').css('background','#e45534');
			        			$('.rightLight').css('background','#009688');
			        		}else if(sign=='02'||sign1=='02'){
			        			$('.leftLight').css('background','#FFD700');//黄灯
			        			$('.rightLight').css('background','#FFD700');
			        		}else if(sign=='03'||sign1=='03'){
			        			$('.leftLight').css('background','#009688');
			        			$('.rightLight').css('background','#e45534');
			        			// console.log("报错");
			        		}else{

			        		}
		 				}else if(direction=='02'){//02水平方向
		 					
		 					if(sign=='01'||sign1=='01'){
			        			$('.leftLight').css('background','#009688');
			        			$('.rightLight').css('background','#e45534');
			        		}else if(sign=='02'||sign1=='02'){
			        			$('.leftLight').css('background','#FFD700');
			        			$('.rightLight').css('background','#FFD700');//黄灯
			        		}else if(sign=='03'||sign1=='03'){
			        			$('.leftLight').css('background','#e45534');
			        			$('.rightLight').css('background','#009688');
			        		}else{

			        		}
			        		
		 				}else{

		 				}

		 				timeLeft=parseInt(greenTime,16);
		 				timeLeft1=parseInt(greenTime1,16);
						$('.leftLight').html(timeLeft+"s");
	 			        $('.rightLight').html(timeLeft+"s");
		        	 }
		        	
	        	
	      	  },
		      error: function () {
		       
		        //$('#receive').text("获取数据失败,请重试！");
		        
		      }
	    })
	  	 //console.log(dataID);
	  	if(dataID==8101){
	     	//clearInterval(timer)
	    	//move();
	    	// clearInterval(timer1)
	    	// move1();
	    }

	 }

	 var arr=[];
	 var speed;
	 //接收位置
	  function receiveLocation(){
	  	 $.ajax({
		      type: "get",
		      url: "http://182.92.6.59:8099/Bus/getShortdata", 
		      dataType: "text",
		      contentType: "application/json",
		      success: function (data) {
	        		var Ldata=data;
	        		var Ldirection=Ldata.substring(0,2);//方向
	        		var Llocation=Ldata.substring(2,4);//距离
	        		//console.log(Llocation);
	        		if(Ldirection=='00'){
	        			positionX=(100-parseInt(Llocation,16))*5.87;
	        			clearInterval(timer)
	    				//move1();
	        		}else if(Ldirection=='01'){
	        			positionX=(100+parseInt(Llocation,16))*5.87;
	        			clearInterval(timer)
	    				//move1();
	        		}else{

	        		}
	        		arr.push(positionX);
					if(arr.length>2){
						arr.shift();
					}
					for(var i=0;i<arr.length;i++){
						if(arr[i]==undefined){
							arr[i]=0;
						}
					}
					speed=(arr[1]-arr[0])/20;
					//console.log(speed);
					if(speed>0){
						move1();
					}
	 				
	      	  },
		      error: function () {
		       	alert('获取距离数据失败')
		        
		      }
		     
	    })
	  
	 }
	

	
	 var timer;
	 var timer1;
	 var x=-73;
	 var y = 240;
	 function move1(){
	 	var bus= new Image();
	 	bus.src='img/bus1.jpg';
	    //定时器
	     timer=setInterval(function(){
	     	if(direction=='02'){//水平方向红灯
	     		if(x>581.13){
		    		x=x+speed;
		    	}else if(x>578.48&&x<581.77){
		    		x=581.13;
		    	}else{
		    		x=x+speed;
		    	}
		    }else{
		    	x=x+speed;
		    }

		    if(x <canvas.width+300){
		          context.drawImage(myimg,0,0);
		          context.beginPath();
				  context.drawImage(bus,x,y,73,20);	
				  context.fill();
		        
		     }
		     
		     if(x>=1297){
		     	x=-76;
		     }
		     console.log(x);
	     },50);
	 
	 }


	 //speed=3;
	 // function move1(){
	 // 	//console.log(x+speed);
	 	
	 // 	var bus= new Image();
	 // 	bus.src='img/bus1.jpg';

	 // 	 timer=setInterval(function(){
	 // 	 	x=x+speed;
		//     if(x <canvas.width+100){
		//           context.drawImage(myimg,0,0);
		//           context.beginPath();
		// 		  context.drawImage(bus,x,y,73,20);	
		// 		  context.fill();
		        
		//      }
	 //     },50);

	 // }



})